<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar v-if="!forSelection" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :width="column.width" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <!-- <span v-html="brightenKeyword(scope.row.strContenet,filters.strContent)" /> -->
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button title="编辑" type="text" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button title="删除" type="text" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <FormDialog class="fixed-dialog-height" title="编辑" :show.sync="dialogFormVisible" width="90%" @on-ok="ok" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" :id="editId" ref="editForm" />
    </FormDialog>
  </div>
</template>

<script>
// secondary package based on el-pagination
// import SelectDialog from '@neu/biz-core/components/SelectDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
// 表格查询组件独立编写，需要传递pageable-table实例的mixin方法filter
import TableFilter from './components/tableFilter'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import { page, remove } from '@neu/dcom-ui/api/knowledge'

export default {
  name: 'InspectionRule',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable
    // SelectDialog
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  props: {
    forSelection: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      r: page,
      d: remove,
      // 巡检类型
      inspectionTypes: [],
      insideColumns: [
        { title: '标题', key: 'title', sortable: 'custom', display: true },
        { title: '阅读量', key: 'readingVolume', sortable: 'custom', display: true },
        { title: '知识库类型', key: 'knowledgeTypeName', sortable: 'custom', display: true },
        { title: '关键字', key: 'keywordsName', display: true, render: (h, params) => {
          let des = ''
          if (Array.isArray(params.row.keywordsName)) {
            params.row.keywordsName.forEach(t => {
              des = des + t + ', '
            })
            des = des.substring(0, des.length - 2)
          } else {
            des = params.row.keywordsName
          }
          return h('span', des)
        }
        },
        { title: '创建人', key: 'userName', sortable: 'custom', display: true },
        { title: '更新人', key: 'updateUserName', sortable: 'custom', display: true },
        { title: '创建时间', key: 'createTime', sortable: 'custom', render: (h, params) => { return h('span', formatDate(params.row.createTime)) }, display: true },
        { title: '更新时间', key: 'updateTime', sortable: 'custom', render: (h, params) => { return h('span', formatDate(params.row.updateTime)) }, display: true }
      ],

      dialogFormVisible: false,

      dialogStatus: 'create',
      editId: -1
    }
  },
  mounted() {
  },
  methods: {
    // 表格编辑按钮点击事件处理
    edit(id) {
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    }
  }
}
</script>
